#!/usr/bin/env python
# -*- coding: utf-8 -*-

from collections import defaultdict
from RGM import RGM as rgm
from helpers import get_sorted_tuple as gst
import sqlite3 as sqlite

# список подписок [(uid, item), ]
subscriptions = rgm

# инвертированный словарь {item: set([uid1, uid2, ]), }
rev_subscriptions = defaultdict(set)
for (uid, item) in subscriptions:
    rev_subscriptions[item].add(uid)

# items, для которых более 1 uid
mini_rev_subscriptions = {}
for item in rev_subscriptions.keys():
    if len(rev_subscriptions[item]) > 5:
        mini_rev_subscriptions[item] = rev_subscriptions[item]

print 'len = %s' % len(mini_rev_subscriptions)

items_n = []

for i in mini_rev_subscriptions.keys():
    items_n.append((i, len(mini_rev_subscriptions[i])))

conn = sqlite.connect('items_index.db')
c = conn.cursor()
c.executemany("insert into items_n (item, n) values (?, ?)", items_n)


conn.commit()
c.close()

